home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / daten / ispell / source / minrexx.c < prev    next >
C/C++ Source or Header  |  1995-06-29  |  15KB  |  457 lines

  1. /*
  2.  *   This is an example of how REXX messages might be handled.  This is
  3.  *   a `minimum' example that both accepts asynchronous REXX messages and
  4.  *   can request REXX service.
  5.  *
  6.  *   Read this entire file!  It's short enough.
  7.  *
  8.  *   It is written in such a fashion that it can be attached to a program
  9.  *   with a minimum of fuss.  The only external symbols it makes available
  10.  *   are the seven functions and RexxSysBase.
  11.  *
  12.  *   This code is by Radical Eye Software, but it is put in the public
  13.  *   domain.  I would appreciate it if the following string was left in
  14.  *   both as a version check and as thanks from you for the use of this
  15.  *   code.
  16.  *
  17.  *   If you modify this file for your own use, don't bump the version
  18.  *   number; add a suffix, such as 1.0a or 1.0.3 or something, so we
  19.  *   don't have fake `versions' floating around.
  20.  */
  21. static char *blurb = "Radical Eye MinRexx 0.4ljr";
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "config.h"
  26. #include "ispell.h"
  27. #include "proto.h"
  28. #include <inline/exec.h>
  29.  
  30. /*
  31.  *   All of our local globals, hidden from sight.
  32.  */
  33. static struct MsgPort *rexxPort;/* this is *our* rexx port */
  34. static int bringerdown;        /* are we trying to shut down? */
  35. static struct rexxCommandList *globalrcl;    /* our command association list */
  36. static long stillNeedReplies;    /* how many replies are pending? */
  37. static long rexxPortBit;    /* what bit to wait on for Rexx? */
  38. static char *extension;        /* the extension for macros */
  39. static void (*userdisp) (struct RexxMsg *, struct rexxCommandList *, char *);    /* the user's dispatch function */
  40. static struct RexxMsg *oRexxMsg;/* the outstanding Rexx message */
  41. /*
  42.  *   Our library base.  Don't you dare close this!
  43.  */
  44. struct RxsLib *RexxSysBase;
  45. /*
  46.  *   This is the main entry point into this code.
  47.  */
  48. long upRexxPort (char *s, struct rexxCommandList *rcl, char *exten, void(*uf)(struct RexxMsg *, struct rexxCommandList *, char *))
  49. /*
  50.  *   The first argument is the name of your port to be registered;
  51.  *   this will be used, for instance, with the `address' command of ARexx.
  52.  *   The second argument is an association list of command-name/user-data
  53.  *   pairs.  It's an array of struct rexxCommandList, terminated by a
  54.  *   structure with a NULL in the name field. The commands are case
  55.  *   sensitive.  The user-data field can contain anything appropriate,
  56.  *   perhaps a function to call or some other data.
  57.  *   The third argument is the file extension for ARexx macros invoked
  58.  *   by this program.  If you supply this argument, any `primitive' not
  59.  *   in the association list rcl will be sent out to ARexx for
  60.  *   interpretation, thus allowing macro programs to work just like
  61.  *   primitives.  If you do not want this behavior, supply a `NULL'
  62.  *   here, and those commands not understood will be replied with an
  63.  *   error value of RXERRORNOCMD.
  64.  *   The fourth argument is the user dispatch function.  This function
  65.  *   will *only* be called from rexxDisp(), either from the user calling
  66.  *   this function directly, or from dnRexxPort().  Anytime a command
  67.  *   match is found in the association list, this user-supplied function
  68.  *   will be called with two arguments---the Rexx message that was
  69.  *   received, and a pointer to the association pair.  This function
  70.  *   should return a `1' if the message was replied to by the function
  71.  *   and a `0' if the default success code of (0, 0) should be returned.
  72.  *   Note that the user function should never ReplyMsg() the message;
  73.  *   instead he should indicate the return values with replyRexxCmd();
  74.  *   otherwise we lose track of the messages that still lack replies.
  75.  *   upRexxPort() returns the signal bit to wait on for Rexx messages.
  76.  *   If something goes wrong, it simply returns a `0'.  Note that this
  77.  *   function is safe to call multiple times because we check to make
  78.  *   sure we haven't opened already.  It's also a quick way to change
  79.  *   the association list or dispatch function.
  80.  */
  81. {
  82.  
  83. /*
  84.  *   Some basic error checking.
  85.  */
  86.   if (rcl == NULL || uf == NULL)
  87.     return (0L);
  88. /*
  89.  *   If we aren't open, we make sure no one else has opened a port with
  90.  *   this name already.  If that works, and the createport succeeds, we
  91.  *   fill rexxPortBit with the value to return.
  92.  *
  93.  *   Note that rexxPortBit will be 0 iff rexxPort is NULL, so the check
  94.  *   for rexxPort == NULL also insures that our rexxPortBit is 0.
  95.  */
  96.   if (rexxPort == NULL)
  97.     {
  98.       Forbid ();
  99.       if (FindPort (s) == NULL)
  100.     rexxPort = CreateMsgPort ();
  101.       rexxPort->mp_Node.ln_Name = s;
  102.       rexxPort->mp_Node.ln_Pri = 0;
  103.       AddPort (rexxPort);
  104.       Permit ();
  105.       if (rexxPort != NULL)
  106.     rexxPortBit = 1L << rexxPort->mp_SigBit;
  107.     }
  108. /*
  109.  *   Squirrel away these values for our own internal access, and return
  110.  *   the wait bit.
  111.  */
  112.   globalrcl = rcl;
  113.   extension = exten;
  114.   userdisp = uf;
  115.   return (rexxPortBit);
  116. }
  117.  
  118. /*
  119.  *   This function closes the rexx library, but only if it is open
  120.  *   and we aren't expecting further replies from REXX.  It's
  121.  *   *private*, but it doesn't have to be; it's pretty safe to
  122.  *   call anytime.
  123.  */
  124. void closeRexxLib (void)
  125. {
  126.   if (stillNeedReplies == 0 && RexxSysBase)
  127.     {
  128.       CloseLibrary ((struct Library *)RexxSysBase);
  129.       RexxSysBase = NULL;
  130.     }
  131. }
  132.  
  133. /*
  134.  *   This function closes down the Rexx port.  It is always safe to
  135.  *   call, and should *definitely* be made a part of your cleanup
  136.  *   routine.  No arguments and no return.  It removes the Rexx port,
  137.  *   replies to all of the messages and insures that we get replies
  138.  *   to all the ones we sent out, closes the Rexx library, deletes the
  139.  *   port, clears a few flags, and leaves.
  140.  */
  141. void dnRexxPort (void)
  142. {
  143.   if (rexxPort)
  144.     {
  145.       RemPort (rexxPort);
  146.       bringerdown = 1;
  147. /*
  148.  *   A message still hanging around?  We kill it off.
  149.  */
  150.       if (oRexxMsg)
  151.     {
  152.       oRexxMsg->rm_Result1 = RXERRORIMGONE;
  153.       ReplyMsg ((struct Message *)oRexxMsg);
  154.       oRexxMsg = NULL;
  155.     }
  156.       while (stillNeedReplies)
  157.     {
  158.       WaitPort (rexxPort);
  159.       dispRexxPort ();
  160.     }
  161.       closeRexxLib ();
  162.       DeleteMsgPort (rexxPort);
  163.       rexxPort = NULL;
  164.     }
  165.   rexxPortBit = 0;
  166. }
  167.  
  168. /*
  169.  *   Here we dispatch any REXX messages that might be outstanding.
  170.  *   This is the main routine for handling Rexx messages.
  171.  *   This function is fast if no messages are outstanding, so it's
  172.  *   pretty safe to call fairly often.
  173.  *
  174.  *   If we are bring the system down and flushing messages, we reply
  175.  *   with a pretty serious return code RXERRORIMGONE.
  176.  *
  177.  *   No arguments, no returns.
  178.  */
  179. void dispRexxPort (void)
  180. {
  181.   register struct RexxMsg *RexxMsg;
  182.   register struct rexxCommandList *rcl;
  183.   register char *p;
  184.   register int dontreply;
  185.  
  186. /*
  187.  *   If there's no rexx port, we're out of here.
  188.  */
  189.   if (rexxPort == NULL)
  190.     return;
  191. /*
  192.  *   Otherwise we have our normal loop on messages.
  193.  */
  194.   while (RexxMsg = (struct RexxMsg *) GetMsg (rexxPort))
  195.     {
  196. /*
  197.  *   If we have a reply to a message we sent, we look at the second
  198.  *   argument.  If it's set, it's a function we are supposed to call
  199.  *   so we call it.  Then, we kill the argstring and the message
  200.  *   itself, decrement the outstanding count, and attempt to close
  201.  *   down the Rexx library.  Note that this call only succeeds if
  202.  *   there are no outstanding messages.  Also, it's pretty quick, so
  203.  *   don't talk to me about efficiency.
  204.  */
  205.       if (RexxMsg->rm_Node.mn_Node.ln_Type == NT_REPLYMSG)
  206.     {
  207.       if (RexxMsg->rm_Args[1])
  208.         {
  209.           (*((int (*) (struct RexxMsg *)) (RexxMsg->rm_Args[1]))) (RexxMsg);
  210.         }
  211.       DeleteArgstring (RexxMsg->rm_Args[0]);
  212.       DeleteRexxMsg (RexxMsg);
  213.       stillNeedReplies--;
  214.       closeRexxLib ();
  215. /*
  216.  *   The default case is we got a message and we need to check it for
  217.  *   primitives.  We skip past any initial tabs or spaces and initialize
  218.  *   the return code fields.
  219.  */
  220.     }
  221.       else
  222.     {
  223.       p = (char *) RexxMsg->rm_Args[0];
  224.       while (*p > 0 && *p <= ' ')
  225.         p++;
  226.       RexxMsg->rm_Result1 = 0;
  227.       RexxMsg->rm_Result2 = 0;
  228. /*
  229.  *   If somehow the reply is already done or postponed, `dontreply' is
  230.  *   set.
  231.  */
  232.       dontreply = 0;
  233. /*
  234.  *   If the sky is falling, we just blow up and replymsg.
  235.  */
  236.       if (bringerdown)
  237.         {
  238.           RexxMsg->rm_Result1 = RXERRORIMGONE;
  239. /*
  240.  *   Otherwise we cdr down our association list, comparing commands,
  241.  *   until we get a match.  If we get a match, we call the dispatch
  242.  *   function with the appropriate arguments, and break out.
  243.  */
  244.         }
  245.       else
  246.         {
  247.           oRexxMsg = RexxMsg;
  248.           for (rcl = globalrcl; rcl->name; rcl++)
  249.         {
  250.           if (cmdcmp (rcl->name, p) == 0)
  251.             {
  252.               if (p[strlen (rcl->name)])
  253.             (*userdisp) (RexxMsg, rcl, p + strlen (rcl->name) + 1);
  254.               else
  255.             (*userdisp) (RexxMsg, rcl, p + strlen (rcl->name));
  256.               break;
  257.             }
  258.         }
  259. /*
  260.  *   If we broke out, rcl will point to the command we executed; if we
  261.  *   are at the end of the list, we didn't understand the command.  In
  262.  *   this case, if we were supplied an extension in upRexxPort, we know
  263.  *   that we should send the command out, so we do so, synchronously.
  264.  *   The synchronous send takes care of our reply.  If we were given a
  265.  *   NULL extension, we bitch that the command didn't make sense to us.
  266.  */
  267.           if (rcl->name == NULL)
  268.         {
  269.           if (extension)
  270.             {
  271.               syncRexxCmd (RexxMsg->rm_Args[0], RexxMsg);
  272.               dontreply = 1;
  273.             }
  274.           else
  275.             {
  276.               RexxMsg->rm_Result1 = RXERRORNOCMD;
  277.             }
  278.         }
  279.         }
  280. /*
  281.  *   Finally, reply if appropriate.
  282.  */
  283.       oRexxMsg = NULL;
  284.       if (!dontreply)
  285.         ReplyMsg ((struct Message *)RexxMsg);
  286.     }
  287.     }
  288. }
  289.  
  290. /*
  291.  *   This is the function we use to see if the command matches
  292.  *   the command string.  Not case sensitive.  Make sure all commands
  293.  *   are given in lower case!
  294.  */
  295. int cmdcmp (char *c, char *m)
  296. {
  297.   while (*c && ((*c == *m) || (*c == *m + 32 && ('a' <= *c && *c <= 'z'))))
  298.     {
  299.       c++;
  300.       m++;
  301.     }
  302.   if (!(*c) && *m)
  303.     return ((int) *m != ' ');
  304.   return ((int) *c);
  305. }
  306.  
  307. /*
  308.  *   Opens the Rexx library if unopened.  Returns success (1) or
  309.  *   failure (0).  This is another function that is *private* but
  310.  *   that doesn't have to be.
  311.  */
  312. int openRexxLib (void)
  313. {
  314.   if (RexxSysBase)
  315.     return (1);
  316.   return ((RexxSysBase = (struct RxsLib *)OpenLibrary (RXSNAME, 0L)) != NULL);
  317. }
  318.  
  319. /*
  320.  *   This is the general ARexx command interface, but is not the one
  321.  *   you will use most of the time; ones defined later are easier to
  322.  *   understand and use.  But they all go through here.
  323.  */
  324. struct RexxMsg *sendRexxCmd (char *s, void (*f)(struct RexxMsg *), STRPTR p1, STRPTR p2, STRPTR p3)
  325. /*
  326.  *   The first parameter is the command to send to Rexx.
  327.  *   The second parameter is either NULL, indicating that the command
  328.  *   should execute asynchronously, or a function to be called when the
  329.  *   message we build up and send out here finally returns.  Please note
  330.  *   that the function supplied here could be called during cleanup after
  331.  *   a fatal error, so make sure it is `safe'.  This function always is
  332.  *   passed one argument, the RexxMsg that is being replied.
  333.  *   These are up to three arguments to be stuffed into the RexxMsg we
  334.  *   are building up, making the values available when the message is
  335.  *   finally replied to.  The values are stuffed into Args[2]..Args[4].
  336.  */
  337. {
  338.   register struct MsgPort *rexxport;
  339.   register struct RexxMsg *RexxMsg;
  340.  
  341. /*
  342.  *   If we have too many replies out there, we just return failure.
  343.  *   Note that you should check the return code to make sure your
  344.  *   message got out!  Then, we forbid, and make sure that:
  345.  *      - we have a rexx port open
  346.  *      - Rexx is out there
  347.  *      - the library is open
  348.  *      - we can create a message
  349.  *      - we can create an argstring
  350.  *
  351.  *   If all of these succeed, we stuff a few values and send the
  352.  *   message, permit, and return.
  353.  */
  354.   if (rexxPort == NULL || stillNeedReplies > MAXRXOUTSTANDING - 1)
  355.     return (NULL);
  356.   RexxMsg = NULL;
  357.   if (openRexxLib () && (RexxMsg =
  358.       CreateRexxMsg (rexxPort, extension, rexxPort->mp_Node.ln_Name)) &&
  359.       (RexxMsg->rm_Args[0] = CreateArgstring (s, (long) strlen (s))))
  360.     {
  361.       RexxMsg->rm_Action = RXCOMM;
  362.       RexxMsg->rm_Args[1] = (STRPTR) f;
  363.       RexxMsg->rm_Args[2] = p1;
  364.       RexxMsg->rm_Args[3] = p2;
  365.       RexxMsg->rm_Args[4] = p3;
  366.       RexxMsg->rm_Node.mn_Node.ln_Name = RXSDIR;
  367.       Forbid ();
  368.       if (rexxport = FindPort (RXSDIR))
  369.     PutMsg (rexxport, (struct Message *)RexxMsg);
  370.       Permit ();
  371.       if (rexxport)
  372.     {
  373.       stillNeedReplies++;
  374.       return (RexxMsg);
  375.     }
  376.       else
  377.     DeleteArgstring (RexxMsg->rm_Args[0]);
  378.     }
  379.   if (RexxMsg)
  380.     DeleteRexxMsg (RexxMsg);
  381.   closeRexxLib ();
  382.   return (NULL);
  383. }
  384.  
  385. /*
  386.  *   This function is used to send out an ARexx message and return
  387.  *   immediately.  Its single parameter is the command to send.
  388.  */
  389. struct RexxMsg *asyncRexxCmd (char *s)
  390. {
  391.   return (sendRexxCmd (s, NULL, NULL, NULL, NULL));
  392. }
  393.  
  394. /*
  395.  *   This function sets things up to reply to the message that caused
  396.  *   it when we get a reply to the message we are sending out here.
  397.  *   But first the function we pass in, which actually handles the reply.
  398.  *   Note how we get the message from the Args[2]; Args[0] is the command,
  399.  *   Args[1] is this function, and Args[2]..Args[4] are any parameters
  400.  *   passed to sendRexxCmd() as p1..p3.  We pass the result codes right
  401.  *   along.
  402.  */
  403. void replytoit (struct RexxMsg *msg)
  404. {
  405.   register struct RexxMsg *omsg;
  406.  
  407.   omsg = (struct RexxMsg *) (msg->rm_Args[2]);
  408.   replyRexxCmd (omsg, msg->rm_Result1, msg->rm_Result2, NULL);
  409.   ReplyMsg ((struct Message *)omsg);
  410. }
  411.  
  412. /*
  413.  *   This function makes use of everything we've put together so far,
  414.  *   and functions as a synchronous Rexx call; as soon as the macro
  415.  *   invoked here returns, we reply to `msg', passing the return codes
  416.  *   back.
  417.  */
  418. struct RexxMsg *syncRexxCmd (char *s, struct RexxMsg *msg)
  419. {
  420.   return (sendRexxCmd (s, &replytoit, (STRPTR)msg, NULL, NULL));
  421. }
  422.  
  423. /*
  424.  *   There are times when you want to pass back return codes or a
  425.  *   return string; call this function when you want to do that,
  426.  *   and return `1' from the user dispatch function so the main
  427.  *   event loop doesn't reply (because we reply here.)  This function
  428.  *   always returns 1.
  429.  */
  430. void replyRexxCmd (struct RexxMsg *msg, long primary, long secondary, char *string)
  431. /*
  432.  *   The first parameter is the message we are replying to.
  433.  *   The next two parameters are the primary and secondary return
  434.  *   codes.
  435.  *   The final parameter is a return string.  This string is only
  436.  *   returned if the primary return code is 0, and a string was
  437.  *   requested.
  438.  *
  439.  *   We also note that we have replied to the message that came in.
  440.  */
  441. {
  442. /*
  443.  *   Note how we make sure the Rexx Library is open before calling
  444.  *   CreateArgstring . . . and we close it down at the end, if possible.
  445.  */
  446.   if (primary == 0 && (msg->rm_Action & (1L << RXFB_RESULT)))
  447.     {
  448.       if (string && openRexxLib ())
  449.     secondary = (long) CreateArgstring (string, (long) strlen (string));
  450.       else
  451.     secondary = 0L;
  452.     }
  453.   msg->rm_Result1 = primary;
  454.   msg->rm_Result2 = secondary;
  455.   closeRexxLib ();
  456. }
  457.